package com.rocky.ssm.controller;

import com.rocky.ssm.model.Comment;
import com.rocky.ssm.service.IEvaluation;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Administrator on 2017/8/9.
 */

@Controller
@RequestMapping("/students")
public class EvaluationController {

    @Autowired
    private IEvaluation evaluation;

    @RequestMapping("/evaluationList")
    public String list(Model model){
        List<Comment> list= evaluation.getCommentList();
        //Comment list = evaluation.getCommentById(1);

        model.addAttribute("evaluationList",list);
        return "evaluation/list";   //返回到jsp页面
    }

    @RequestMapping("/{id}/evaluationUpdate")
    public String update(@PathVariable int id, Model model){
        Comment comment = evaluation.getCommentById(id);
        model.addAttribute("comment",comment);
        return "evaluation/update";  //返回到页面
    }

    @RequestMapping(value = "/doUpdate",method = RequestMethod.POST)
    public String doUpdate(Comment comment){

        if (evaluation.update(comment)>0)
            return "redirect:/students/evaluationList";
        else
            return "evaluation/update";
    }


    @RequestMapping("/{id}/evaluationDel")
    public String delete(@PathVariable int id){
        evaluation.delete(id);
        return "redirect:/students/evaluationList";
    }


    @RequestMapping(value = "/evaluationAdd",method = RequestMethod.POST)
    public String add(Comment comment){
        if(evaluation.save(comment)>0)
            return "redirect:/students/evaluationList";
        else
            return "evaluation/add";
    }

    @RequestMapping("/skipAdd")
    public String skipAdd(){
        return "evaluation/add";
    }
}
